﻿#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Web.Enhancements.Rest.Templates
{
    /// <summary>
    /// Interface that class must implement to be able to return data through REST services that should be 
    /// processed as a static file to respond back to the client.
    /// </summary>
    public interface IStaticFileResult : ITemplatedResult
    {
        /// <summary>
        /// Gets or sets file path that should be rendered within response.
        /// </summary>
        string FilePath { get; set; }
        /// <summary>
        /// Gets or sets the HTTP MIME type that response will write to the stream upon method completion.
        /// </summary>
        string ContentType { get; set; }
        /// <summary>
        /// Gets or sets the HTTP Content-Disposition header that response will write to the stream upon method completion.
        /// </summary>
        string ContentDisposition { get; set; }
    }

    /// <summary>
    /// Class that provides ability to return data through REST services that should be 
    /// processed as a static file to respond back to the client.
    /// </summary>
    public class StaticFileResult : IStaticFileResult
    {
        /// <summary>
        /// Gets or sets file path that should be rendered within response.
        /// </summary>
        public string FilePath { get; set; }
        /// <summary>
        /// Gets or sets the HTTP MIME type that response will write to the stream upon method completion.
        /// <para>Only one setting should be supplied - either this or via WebRestMethodAttribute.</para>
        /// </summary>
        public string ContentType { get; set; }
        /// <summary>
        /// Gets or sets the HTTP Content-Disposition header that response will write to the stream upon method completion. 
        /// <para>Only one setting should be supplied - either this or via WebRestMethodAttribute.</para>
        /// </summary>
        public string ContentDisposition { get; set; }

        /// <summary>
        /// Default constructor.
        /// </summary>
        public StaticFileResult()
        {
            this.FilePath = "";
            this.ContentDisposition = "";
            this.ContentType = "";
        }

        /// <summary>
        /// Method renders static file content into given HTTP response object.
        /// </summary>
        /// <param name="response">HTTP response object.</param>
        public void RenderResult(System.Web.HttpResponse response)
        {
            if (this.ContentType.Length > 0)
                response.ContentType = this.ContentType; //override ContentType if supplied.
            if (this.ContentDisposition.Length > 0)
                response.AppendHeader("Content-Disposition", this.ContentDisposition);
            else if (WebRestServiceContext.Current.ContentDisposition.Length > 0)
                response.AppendHeader("Content-Disposition", WebRestServiceContext.Current.ContentDisposition);
            response.WriteFile(this.FilePath);
        }
    }
}
